import { PlusOutlined } from '@ant-design/icons';
import { Button, Card, Col, Form, Input, InputNumber, Modal, Popconfirm, Row, Space } from 'antd';
import Table, { ColumnsType } from 'antd/es/table';
import { TableRowSelection } from 'antd/es/table/interface';
import { useEffect } from 'react';

import { IconButton, Iconify } from '@/components/icon';
import { useCommon } from '@/hooks/common';

export default function DesignCatePage() {
  const formParams = {
    name: '',
    id: 0,
    parentId: 0,
    disable: 0,
    sort: 1,
  };
  const cateName = '设计分组';
  const {
    data = [],
    ModalPros,
    searchForm,
    onSearchFormReset,
    onCreate,
    // onSwitch,
    doDel,
    onEdit,
    onSearch,
  } = useCommon(
    { name: '', parentId: 0 },
    '',
    {
      ...formParams,
    },
    'design-template-group',
  );

  const columns: ColumnsType<any> = [
    { title: '名称', dataIndex: 'name', width: 300 },
    { title: '排序', dataIndex: 'sort', align: 'center', width: 60 },
    // {
    //   title: '状态',
    //   dataIndex: 'disable',
    //   align: 'center',
    //   width: 120,
    //   render: (disable, record) => (
    //     <Switch checked={disable === 1} onChange={() => onSwitch(record.id)} />
    //   ),
    // },
    {
      title: '操作',
      key: 'operation',
      align: 'center',
      width: 100,
      render: (_, record) => (
        <div className="flex w-full justify-center text-gray">
          <PlusOutlined onClick={() => onCreate({ ...formParams, parentId: record.id })} />
          <IconButton onClick={() => onEdit(record)}>
            <Iconify icon="solar:pen-bold-duotone" size={18} />
          </IconButton>
          <Popconfirm
            title="确认删除该数据？"
            okText="确定"
            cancelText="取消"
            placement="left"
            onConfirm={() => doDel(record.id)}
          >
            <IconButton>
              <Iconify icon="mingcute:delete-2-fill" size={18} className="text-error" />
            </IconButton>
          </Popconfirm>
        </div>
      ),
    },
  ];

  // rowSelection objects indicates the need for row selection
  const rowSelection: TableRowSelection<any> = {
    onChange: (selectedRowKeys, selectedRows) => {
      console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
    },
    onSelect: (record, selected, selectedRows) => {
      console.log(record, selected, selectedRows);
    },
    onSelectAll: (selected, selectedRows, changeRows) => {
      console.log(selected, selectedRows, changeRows);
    },
  };

  return (
    <Space direction="vertical" size="large" className="w-full">
      <Card>
        <Form form={searchForm} onFinish={onSearch}>
          <Row gutter={[16, 16]}>
            <Col span={24} lg={6}>
              <Form.Item<any> label="名称" name="name" className="!mb-0">
                <Input />
              </Form.Item>
            </Col>
            <Col span={24} lg={12}>
              <div className="flex justify-end">
                <Button onClick={onSearchFormReset}>重置</Button>
                <Button type="primary" htmlType="submit" className="ml-4">
                  查询
                </Button>
              </div>
            </Col>
          </Row>
        </Form>
      </Card>

      <Card title={cateName}extra={
          <div className="flex flex-row gap-[10px]">
            <Button
              type="primary"
              onClick={() =>
                onCreate({
                  ...formParams,
                  parentId: 0,
                })
              }
            >
              新增
            </Button>
          </div>
        }>
        <Table
          rowKey="id"
          size="small"
          scroll={{ x: 'max-content' }}
          columns={columns}
          dataSource={data}
          childrenColumnName="sonList"
          rowSelection={{ ...rowSelection }}
        />
      </Card>

      <CommonModal {...ModalPros} />
    </Space>
  );
}

function CommonModal({ title, show, formValue, onOk, onCancel }: any) {
  const [form] = Form.useForm();
  useEffect(() => {
    form.setFieldsValue({ ...formValue });
  }, [formValue, form]);
  return (
    <Modal
      title={title}
      open={show}
      onOk={() => onOk({ ...formValue, ...form.getFieldsValue() })}
      onCancel={onCancel}
    >
      <Form
        initialValues={formValue}
        form={form}
        labelCol={{ span: 4 }}
        wrapperCol={{ span: 18 }}
        layout="horizontal"
      >
        <Form.Item<any> label="名称" name="name" required>
          <Input />
        </Form.Item>
        <Form.Item<any> label="排序" name="sort" required>
          <InputNumber style={{ width: '100%' }} />
        </Form.Item>
      </Form>
    </Modal>
  );
}
